package StackAndQueue;

import java.util.ArrayDeque;

public class isValid20 {
    public boolean isValid(String s) {
        ArrayDeque<Character> deque = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '('){
                deque.push(')');
            }else if(ch == '{'){
                deque.push('}');
            }else if(ch == '['){
                deque.push(']');
            }else if(deque.isEmpty() || deque.peek() != ch){
                return false;
            }else{
                deque.pop();
            }
        }
        return deque.isEmpty();
    }
}
